package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.BuildBuildingV1;
import com.xique.door.bean.domain.OpenDoorRecord;
import com.xique.door.mapper.OpenDoorRecordMapper;
import com.xique.door.project.miniprogram.bean.request.MiniOpenDoorRecordRequest;
import com.xique.door.service.IOpenDoorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2022/4/20 14:58
 */
@Service
public class OpenDoorRecordServiceImpl extends ServiceImpl<OpenDoorRecordMapper, OpenDoorRecord> implements IOpenDoorRecordService {

    @Autowired
    private OpenDoorRecordMapper openDoorRecordMapper;

    @Override
    public List<OpenDoorRecord> selectRecordList(MiniOpenDoorRecordRequest request) {

        QueryWrapper<OpenDoorRecord> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getUserId())) {
            condition.eq("user_id", request.getUserId());
        }
//        if (StringUtils.isNotNull(request.getStartTime()) && StringUtils.isNotNull(request.getEndTime())) {
//            condition.between("open_time", request.getStartTime(), request.getEndTime());
//        }
        condition.orderByDesc("open_time");

        return openDoorRecordMapper.selectList(condition);
    }

    @Override
    public IPage<OpenDoorRecord> selectRecordPage(MiniOpenDoorRecordRequest request) {
        QueryWrapper<OpenDoorRecord> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getUserId())) {
            condition.eq("user_id", request.getUserId());
        }
        if (StringUtils.isNotNull(request.getParkId())) {
            condition.eq("park_id", request.getParkId());
        }
        Page<OpenDoorRecord> page = new Page<>();
        if (StringUtils.isNotNull(request.getPageNum()) && StringUtils.isNotNull(request.getPageSize())) {
            page.setCurrent(request.getPageNum());
            page.setSize(request.getPageSize());
        }
        condition.orderByDesc("open_time");
        return openDoorRecordMapper.selectPage(page, condition);
    }
}
